﻿@using System.Collections.Generic
@using System.Linq
@using MrCMS.Helpers
@using MrCMS.Paging
@using MrCMS.Settings
@using MrCMS.Tasks
@model MrCMS.Tasks.QueuedTaskSearchQuery
@{
    ViewBag.Title = "Scheduled Tasks";
    List<ScheduledTask> scheduledTasks = ViewData["scheduled-tasks"] as List<ScheduledTask> ?? new List<ScheduledTask>();
    IPagedList<QueuedTask> tasks = ViewData["tasks"] as IPagedList<QueuedTask> ?? PagedList<QueuedTask>.Empty;
    var siteSettings = SiteSettings<SiteSettings>();
    string executionUrl = Url.Action("Execute", "TaskExecution", new RouteValueDictionary {{"area", ""}, {siteSettings.TaskExecutorKey, siteSettings.TaskExecutorPassword}}, "http");
}

@Html.InfoBlock(Resource("Heads up") + "!", string.Format(Resource("In order to execute tasks, you must poll the following url: <a href='{0}'>{1}</a> <br />This will need to be polled by some sort of external task scheduler or cron system in order to run regularly."), executionUrl, executionUrl))

<fieldset>
    <legend>Scheduled Tasks
        <span class="btn-group display-inline pull-right">
            @Html.ActionLink("Add", "Add", null, new {@class = "btn btn-primary", data_toggle = "fb-modal"})
        </span>
    </legend>

    @if (scheduledTasks.Any())
    {
            <table class="table table-striped table-condensed margin-top-0">
                <thead>
                    <tr>
                        <th>Type
                        </th>
                        <th>Every X Seconds
                        </th>
                        <th>Status
                        </th>
                        <th>Last Completed
                        </th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (ScheduledTask task in scheduledTasks)
                    {
                        <tr>
                            <td>
                                @task.TypeName
                            </td>
                            <td>
                                @task.EveryXSeconds
                            </td>
                            <td>
                                @task.Status
                            </td>
                            <td>
                                @(task.LastComplete.HasValue ? task.LastComplete.Value.ToString("dd/MM/yyyy HH:mm:ss") : "-")
                            </td>
                            <td>
                                <div class="btn-group">
                                    @Html.ActionLink("Edit", "Edit", new {id = task.Id}, new {@class = "btn btn-xs btn-default", data_toggle = "fb-modal"})
                                    @Html.ActionLink("Delete", "Delete", new {id = task.Id}, new {@class = "btn btn-danger btn-xs", data_toggle = "fb-modal"})
                                </div>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
    }
    else
    {
        @:Please click 'Add' to add your first scheduled task.
    }
</fieldset>
<fieldset>
    <legend>Queued Tasks
        <span class="btn-group display-inline pull-right"></span>
    </legend>
    @if (tasks.Any())
    {
        <table class="table table-striped table-condensed margin-top-0">
            <thead>
                <tr>
                    <th>Added On
                    </th>
                    <th>Type
                    </th>
                    <th>Status
                    </th>
                    <th>Completed at
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach (QueuedTask task in tasks)
                {
                    <tr>
                        <td>
                            @task.CreatedOn.ToString("dd/MM/yyyy HH:mm")
                        </td>
                        <td>
                            @task.DisplayTypeName
                        </td>
                        <td>
                            @task.Status
                        </td>
                        <td>
                            @if (task.CompletedAt.HasValue)
                            {
                                @task.CompletedAt.Value.ToString("dd/MM/yyyy HH:mm")
                            }
                            else
                            {
                                @:-
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
        @Html.Pager(tasks, "Index", page => new {page})
    }
    else
    {
        @:No queued tasks have matched your query
    }
</fieldset>